﻿using System;
using System.Collections.Generic;
using OpenTK;

namespace Chimp.Core
{
    public class ChimpParticleEmitter : ChimpBase
    {
        private int pID;
        private Random rnd = new Random(Environment.TickCount);
        public void AddType(ChimpParticle p)
        {
            this.ParticleList.Add(p);
        }
        public ChimpParticleEmitter()
        {
            LayerID = 0;
            ParticleList = new List<ChimpParticle>();
            RateMin = 50;
            RateMax = 80;
            MaxSpawn = 5000;
            Direction = 0.0f;
            EnergyMin = 1.0f;
            EnergyMax = 2.0f;
            ScaleMin = 0.2f;
            ScaleMax = 1.0f;
            XRnd = 3.0f;
            YRnd = 3.0f;
            ZVel = 1.0f;
            LocalForce = new Vector2(0, 5);
            WorldForce = new Vector2(0, 0);
            XRange = 3.0f;
            YRange = 3.0f;
            Decline = 0.96f;
            ZMin = 0.01f;
            ZMax = 0.1f;
            Emit = true;
            ColorLow = 0.9f;
            ColorHigh = 1.1f;
            ColorRange = 0.1f;
            ColorSpeedMax = 0.1f;
            ColorSpeedMin = 0.05f;
        }

        public List<ChimpParticle> ParticleList { get; set; }
        public bool Emit { get; set; }
        public int RateMin { get; set; }
        public int RateMax { get; set; }
        public float Direction { get; set; }
        public float EnergyMin { get; set; }
        public float EnergyMax { get; set; }
        public int MaxSpawn { get; set; }
        public Vector2 LocalForce { get; set; }
        public Vector2 WorldForce { get; set; }
        public float XRange { get; set; }
        public float YRange { get; set; }
        public float XRnd { get; set; }
        public float YRnd { get; set; }
        public float Decline { get; set; }
        public float ScaleMin { get; set; }
        public float ScaleMax { get; set; }
        public float ZVel { get; set; }
        public float ZMin { get; set; }
        public float ZMax { get; set; }
        public float ColorLow { get; set; }
        public float ColorHigh { get; set; }
        public float ColorRange { get; set; }
        public float ColorSpeedMin { get; set; }
        public float ColorSpeedMax { get; set; }
        public int LayerID { get; set; }

        public void Burst(int num)
        {
            for (int i = 0; i < num; i++)
            {
                SpawnParticle();
            }
        }

        public override bool OnUpdate(Vector2 pos)
        {
            rnd = new Random(Environment.TickCount);

            if (Emit)
            {
                int pc = rnd.Next(RateMin, RateMax);

                for (int i = 0; i < pc; i++)
                {
                    SpawnParticle();
                }
            }
            return false;
        }

        public void SpawnParticle()
        {
            int pn = rnd.Next(0, ParticleList.Count);
            if (pn >= ParticleList.Count) pn = ParticleList.Count - 1;
            ChimpParticle p = ParticleList[pn];
            ChimpParticle oop = p;
            ChimpParticle np = p.RequestNew();
            np.Visual = p.Visual;
            p = np;
            p.Decline = Decline;
            p.Energy = EnergyMin + ((float) rnd.NextDouble()*(EnergyMax - EnergyMin));
            p.I = new Vector2(((float) rnd.NextDouble()*XRnd) - XRnd/2.0f, ((float) rnd.NextDouble()*YRnd) - YRnd/2.0f);
            Vector2 lf = LocalForce;
            float d = Direction;
            p.I += ChimpMath.Rotate(ref lf, ref d);
            p.I += WorldForce;
            p.Pos = new Vector2(Pos.X, Pos.Y) +
                    new Vector2(((float) rnd.NextDouble()*XRange) - XRange/2.0f,
                                ((float) rnd.NextDouble()*YRange) - YRange/2.0f);
            p.Scale = ScaleMin + (float) rnd.NextDouble()*(ScaleMax - ScaleMin);
            p.ID = ID + "p" + pID;
            p.ZVel = ZVel;
            p.ColorLow = ColorLow + (-ColorRange/2.0f + (float) rnd.NextDouble()*ColorRange);
            p.ColorHigh = ColorHigh + (-ColorRange/2.0f + (float) rnd.NextDouble()*ColorRange);
            p.ColorMod = p.ColorLow + (float) rnd.NextDouble()*(p.ColorHigh - p.ColorLow);
            p.LayerID = LayerID;
            p.ColorSpeed = ColorSpeedMin + (float) rnd.NextDouble()*(ColorSpeedMax - ColorSpeedMin);
            p.Levels = oop.Levels;
            p.Ranges = oop.Ranges;
            p.Z = ZMin + (float) rnd.NextDouble()*(ZMax - ZMin);
            Scene.AddObj(p);

            pID++;
        }

        public override void OnDraw(Vector2 pos)
        {
        }
    }
}